

%include "../sasoptions.sas";

** ALL DIRECTORIES LIVE HERE -- READS IN DIRECTORIES;
%include "../set_up_directory.sas";

* define libraries;
libname medex  "&medparExt.";
libname indxex "&indxExt.";
libname carex  "&carext.";
libname opex "&opext.";
%include "&SASmac./UtilityMacros.sas";
%include "&SASmac./read_sysparm.sas";

******************************************
** Process Carrier Ambulance Claims
******************************************;

%macro CarAmbProcess(cohrt=,pct=);
    proc contents data = carex.&cohrt.&pct._carlnits;
        
    data caramb; 
        set carex.&cohrt.&pct._carlnits;
        where plcsrvc="41" & dgn_date = from_dt & mdfr_cd1 in ("RH","SH","IH","EH","NH","JH","PH");

    data carex.&cohrt.&pct._ambclms; 
        set carex.&cohrt.&pct._carlnits;
        where plcsrvc="41" & dgn_date = from_dt & mdfr_cd1 in ("RH","SH","IH","EH","NH","JH","PH");; 
        
    proc sort data = caramb nodupkey;
        by diag_id dgn_date;
        
    data carex.&cohrt.&pct._caramb (keep = bene_id dgn_date diag_id mdfr_cd1 amb_file rename = (mdfr_cd1= amb_origdest));
        set caramb;
        amb_file = "car"; 
        
    proc freq data = carex.&cohrt.&pct._caramb; 
        tables amb_origdest; 

    %mend;

******************************************
** Process Outpatient Ambulance Claims
******************************************;

%macro CarOpProcess(cohrt=,pct=);
    proc contents data = opex.&cohrt.&pct._oprevcntrs;
    proc contents data = opex.&cohrt.&pct._opclms;        

    data opamb; 
        set opex.&cohrt.&pct._oprevcntrs;
        where REV_CNTR="0540" and dgn_date = rev_dt  and mdfr_cd1 in ("RH","SH","IH","EH","NH","JH","PH"); 

    data opamb2; 
        set opex.&cohrt.&pct._opclms;
proc contents data = opamb;
    proc contents data = opamb2;
run;        
    proc sort data = opamb2; by fileyear part clm_id ;
    proc sort data = opamb; by fileyear part clm_id ;         

    data opex.&cohrt.&pct._ambclms;
        merge opamb (in=a) opamb2 (in=b);
        by fileyear part clm_id ;
        if a and b;
        
    proc sort data = opamb nodupkey;
        by diag_id dgn_date;
        
    data opex.&cohrt.&pct._opamb (keep = bene_id dgn_date diag_id mdfr_cd1 amb_file rename = (mdfr_cd1= amb_origdest));
        set opamb;
        amb_file = "op";
        
    proc freq data = opex.&cohrt.&pct._opamb;

        tables amb_origdest; 

    proc contents data = opex.&cohrt.&pct._opamb;        
    %mend;

******************************************
** Combine Ambulance Index Events
******************************************;

%macro CombineAmbIndx (cohrt=,pct=);
    data combined; 
        set  opex.&cohrt.&pct._opamb carex.&cohrt.&pct._caramb;

    proc sort data = combined nodupkey;
        by diag_id;
        
    data indxex.&cohrt.&pct._ambindx;
        set combined;
        
    proc freq data = indxex.&cohrt.&pct._ambindx;
        tables amb_origdest amb_file;        
    run;
    
    *Export to Stat/Transfer;
    x "st &indxExt./&cohrt.&pct._ambindx.sas7bdat
          &indxExt./&cohrt.&pct._ambindx.dta -y -od";
    x "st &carext./&cohrt.&pct._ambclms.sas7bdat
        &carext./&cohrt.&pct._ambclms.dta -y -od";
    x "st &opext./&cohrt.&pct._ambclms.sas7bdat
        &opext./&cohrt.&pct._ambclms.dta -y -od";
run;
    %mend; 

**BeginWrapperPrograms************************;
%macro ExtAmbulance(cohrt1=,cohrt2=,cohrt3=,cohrt4=,cohrt5=,cohrtN=,pct=05);
    %do j=1 %to &cohrtN.;
        %CarAmbProcess(cohrt=&&cohrt&j..,pct=&pct.);
        %CarOpProcess(cohrt=&&cohrt&j..,pct=&pct.);
        %CombineAmbIndx(cohrt=&&cohrt&j..,pct=&pct.);        
        %end;
    %mend;
*****END WRAPPER********************************;



***CALL MACRO**********************************;
*sampleRule-- is equal to mn by default unless you decide create several differnt extract using different tags. ;
%ExtAmbulance(cohrt1=&cohrt.,cohrtN=1,pct=&pct.);
*%ExtComorbidCC(cohrt1=cms,cohrtN=1,pct=20);
